<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <div style="background: #fff; padding: 13px 15px;">
            <span :class="[state.type == '' ? 'btn btn-info' : 'btn']" @click="changeType('')">{{ t('全部') }}</span>
            <span :class="[state.type == '出口' ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeType('出口')">{{ t('出口') }}</span>
            <span :class="[state.type == '进口' ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeType('进口')">{{ t('进口') }}</span>
            <span :class="[state.type == '内销' ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeType('内销')">{{ t('内销') }}</span>
        </div>

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader :buttons="['add', 'delete', 'quickSearch', 'columnDisplay']" :quick-search-placeholder="t('搜索')">
            
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        
        <Table ref="tableRef">

            <template #type>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('类型')" align="left" width="auto">
                    <template #default="scope">
                        <span v-for="(item, index) in scope.row.type" :key="index">
                            <el-tag type="primary" style="margin-right: 5px;" v-if="item == '进口'">{{ t(item) }}</el-tag>
                            <el-tag type="success" style="margin-right: 5px;" v-if="item == '出口'">{{ t(item) }}</el-tag>
                            <el-tag type="danger" style="margin-right: 5px;" v-if="item == '内销'">{{ t(item) }}</el-tag>
                        </span>
                    </template>
                </el-table-column>
            </template>
            
            <template #corp_name>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('公司名称')" align="center">
                    <template #default="scope">
                        <div>{{ scope.row.corp_name }}</div>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />

        <!-- <Quotation v-if="quotation.id != 0" :data="quotation" @closeViewQuotation="closeViewQuotation" /> -->

    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Quotation from './quotation/index.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

import { setStatusApi } from '/@/api/most/business/business'



defineOptions({
    name: 'customer/index',
})

const { t } = useI18n()
const tableRef = ref()




const state = reactive({
    showImportFrom: false,
    loading: false,
    type: '',
    sub_status: {
        count_1: 0,
        count_2: 0,
        count_3: 0,
        count_4: 0
    }
});

const quotation = reactive({
    id: 0,
    name: '',
})

const edit = (scope: anyObj) => {
    baTable.onTableAction('edit', scope)
}

const add = (scope: anyObj) => {
    baTable.form.operate = 'AddProcess'
    baTable.form.items!.id = scope.row.id
    baTable.form.items!.name = scope.row.name
}

const del = (scope: anyObj) => {
    baTable.onTableAction('delete', scope)
}

const clickTian = (row: anyObj) => {
    baTable.table.filter!.id = row.id;
    baTable.table.filter!.pid = row.pid;
    baTable.getIndex();
}

/**
 * 切换类型
 */
const changeType = (type: string) => {
    state.type = type;
    baTable.table.filter!.type = type;
    baTable.getIndex();
}


/**
 * 设置状态 
 */
const setStatus = (id: number, status: number, row: anyObj) => {
    let data = {id: id, status: status}
    setStatusApi(data).then((e) => {
        row.status = e.data.status;
        state.sub_status = e.data.sum_status
    })
}

import router from '/@/router/index'

let optButtons = defaultOptButtons([])


/**
 * 关闭
 */
const closeViewQuotation = () => {
    quotation.id = 0;
}

// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: t('编辑'),
        type: 'primary',
        class: 'btn btn-primary btn-small',
        icon: '',
    },{
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: t('删除'),
        type: 'danger',
        class: 'btn btn-danger mt-0 btn-small',
        icon: '',
        popconfirm: {
            confirmButtonText: t('Delete'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            title: t('Are you sure to delete the selected record?'),
        }
    },{
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: '', // 按钮名称
        title: '', // 鼠标放置时的 title 提示
        class: 'btn mt-8',
        text: '添加进程', // 直接在按钮内显示的文字，title 有值时可为空
        type: 'info', // 按钮类型，请参考 element plus 的按钮类型
        icon: '', // 按钮 icon
        disabledTip: true, // tipButton 禁用 tip
        click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
            baTable.form.operate = 'PlaceOrder';
            baTable.form.items!.id = row.id;
            baTable.form.items!.name = row.name;
            baTable.form.items!.unit = row.unit;
            baTable.form.items!.pay_type = -1;

        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return false;
        },
        disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
            return false;
        },
        attr: {} // 自定义el-button属性
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)

// const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])





/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/system/Customer/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('类型'), prop: 'id', align: 'center', render: 'slot', slotName: 'type' },
            { label: t('公司名称'), prop: 'id', align: 'center', render: 'slot', slotName: 'corp_name' },
            { label: t('国家'), prop: 'country', align: 'center' },
            { label: t('负责人'), prop: 'manager', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('联系电话'), prop: 'contact', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('地区'), prop: 'area', align: 'center' },
            { label: t('邮编'), prop: 'zip_code', align: 'center' },
            // { label: t('营业执照'), render: 'image', prop: 'license', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('添加时间'), prop: 'create_time', align: 'center', render: 'datetime', timeFormat: 'yy-mm-dd' },
            { label: t('操作'), align: 'center', render: 'buttons', buttons: optButtons, operator: false, width: 140 },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: {  },
    },{
        
    },{
        getIndex: ({ res }: { res: ApiResponse }) => { // 查看后
            state.sub_status = res.data.sum_status;
            // console.log(res.data);
            // console.log(state.sub_status)
        }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">

:deep(.el-image){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    // max-width: 80px;
    // max-height: 80px;
}
:deep(td.business-name > div.cell) {
    min-height: 80px;
    display:flex;
    justify-content:center; 
    align-items:center;
}
.tian{
    cursor: pointer;
}
.tian:hover{
    color: #409EFF;
}

:deep(.el-image > img){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    max-width: 80px;
    max-height: 80px;
}
:deep(.ba-data-table .btn){
    padding: 4px 14px;
}


:deep(.ba-data-table .margin-left-none:last-child){
    margin-bottom: 0px;
}
:deep(.table-operate-text){
    padding-left: 0;
}
:deep(.font-size-13){
    font-size: 13px;
}
:deep(.pay-type .el-radio){
    margin-right: 10px;
}
pre{
    font-family: 'Helvetica Neue',Helvetica,'PingFang SC','Hiragino Sans GB','Microsoft YaHei','微软雅黑',Arial,sans-serif;
}
:deep(.ba-operate-dialog .el-dialog__body){
    padding-bottom: 0;
    padding-left: 0;
    padding-right: 0;
}
:deep(.quotation-scrollbar .el-scrollbar:first-child > .el-scrollbar__wrap){
    height: 100%;
}
:deep(.ml-0){
    margin-left: 0!important;
}
:deep(.mt-8){
    margin-top: 8px;
}
:deep(.ml--6){
    margin-left: -6px;
}
:deep(.ml-5){
    margin-left: 5px;
}
:deep(.mt-5){
    margin-top: 5px;
}
:deep(.ba-operate-dialog .el-dialog__body){
    // height: calc(90vh - 55px);
}
</style>
